package com.silverbox.sweeporder.controller.auth;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.silverbox.sweeporder.common.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.silverbox.core.result.CoreResponse;
import com.silverbox.core.result.CoreResponse.StatusInfo;
import com.silverbox.core.util.StringUtils;
import com.silverbox.core.util.Utils;
import com.silverbox.sweeporder.controller.auth.vo.outvo.SmallProgramAuthVO;
import com.silverbox.sweeporder.controller.index.IndexController;
import com.silverbox.sweeporder.service.index.AuthService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("mini")
@Api(tags = { "小程序相关功能" })
public class MiniController {
	
	private static Logger logger = LoggerFactory.getLogger(MiniController.class);
	
	@Value("${sweepOrderVueUrl}")
	private String sweepOrderVueUrl;
	
	@Autowired
	private AuthService authService;
	
	/**
	 * 支付宝小程序授权接口
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = { "/miniProgramAuth" },method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "支付宝小程序授权")
	public CoreResponse<SmallProgramAuthVO> miniProgramAuth(HttpServletRequest request,@RequestParam(required = true) Integer shopperId,@RequestParam(required = true) Integer storeId,@RequestParam(required = true) String authCode,@RequestParam(required = true) Long clientOrderModel){
		//子店铺id
		String path = "";
		CoreResponse<SmallProgramAuthVO> res = new CoreResponse<SmallProgramAuthVO>();
		SmallProgramAuthVO authVO = new SmallProgramAuthVO();
		if(storeId==null || StringUtils.isEmpty(authCode) || shopperId == null){
			path =sweepOrderVueUrl+"/#/error";
			//logger.info("小程序授权参数异常");
			res.setStatusInfo(StatusInfo.ERROR_40001);
			authVO.setSuccess(false);
			authVO.setUrl(path);
			res.setData(authVO);
			return res;
		}
		Integer bindType = Constants.orderOutSupportId.WAIMAI_ORDER.getValue().equals(clientOrderModel) ? 10 : null;
		path = authService.alipayAuth(storeId,shopperId,authCode,bindType);
		String url = sweepOrderVueUrl+path;
		logger.info("小程序授权回调后返回前端页面地址为url="+url);
		authVO.setUrl(url);
		res.setData(authVO);
		return res;
	}
	
	/**
	 * 小程序进入vue扫码下单获取前端页面跳转路径
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "miniProgramRedirectPath",method=RequestMethod.GET)
	@ResponseBody
	 @ApiOperation(value = "小程序进入vue扫码下单获取前端页面跳转路径")
	public void miniProgramRedirectPat(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = true) Integer storeId, @RequestParam(required = true) Long thirdUserInfoId,
			@RequestParam(required = true) Integer source, @RequestParam(required = false) Integer tableInfoId,
			@RequestParam(required = false) String tableName, @RequestParam(required = false) Integer orderSource,
           @RequestParam(required = false) Integer bindType) {
		String path = "";
		try {
			logger.info("小程序进入vue扫码下单参数storeId=" + storeId + "&thirdUserInfoId=" + thirdUserInfoId + "&source=" + source
					+ "&tableInfoId=" + tableInfoId+"&orderSource="+orderSource+"&bindType="+bindType);
			if (storeId == null || thirdUserInfoId == null || source == null) {
				path = sweepOrderVueUrl + "/#/error";
			} else {
				path = authService.miniProgramRedirectPath(storeId, tableInfoId, thirdUserInfoId, tableName,orderSource,source,bindType);
			}
			//source==0支付宝小程序
			if (path.indexOf("?") != -1) {
				path = path + "&mode=" + Integer.valueOf(source);
			} else {
				path = path + "?mode=" + Integer.valueOf(source);
			}
			response.sendRedirect(path);
			logger.info("小程序跳转到VUE判断路径跳转==============" + path);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Utils.log4jDetail(e));
			logger.error("小程序跳转到VUE判断路径跳转==============" + path, Utils.log4jDetail(e));
		}
	}
}
